package vn.edu.saigontech.ncms.web.offering;

import java.util.Collection;
import java.util.Date;

import vn.edu.saigontech.ncms.model.info.Offering;
import vn.edu.saigontech.ncms.service.OfferingService;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.web.AbstractValidator;

@SuppressWarnings("serial")
public class ValidateOfferingAction extends AbstractValidator {
	
	private Long id;
	private String name;
	private Long levelId;
	private Long shiftId;
	private String startDate;
	private String fromDate;
	private String toDate;
	private String endDate;
	private String midtermWrittenDate;
	private String finalSpokenDate;
	private String finalWrittenDate;
	private String cambridgeSpokenDate;
	private String cambridgeWrittenDate;
	private String issueDate;
	
	public String check() {
		Offering offering = OfferingService.INSTANCE.findById(this.id);
		if(!Checker.isNull(this.id)) {
			if(offering.getVersion() != this.version) {
				this.sendKeyError("validator.concurrency", "offering");
			}
		}
		if(Checker.isEmpty(this.name)) {
			this.sendKeyError("validator.required", "name");
		}
		if(Checker.isNull(this.levelId)) {
			this.sendKeyError("validator.required", "level");
		}
		if(Checker.isEmpty(this.startDate)) {
			this.sendKeyError("validator.required", "startDate");
		}
		if(!Checker.isDate(this.startDate, this.i18n("format.date"))) {
			this.sendKeyError("validator.mask", "startDate", "format.date");
		}
		if(Checker.isEmpty(this.endDate)) {
			this.sendKeyError("validator.required", "endDate");
		}
		if(!Checker.isDate(this.endDate, this.i18n("format.date"))) {
			this.sendKeyError("validator.mask", "endDate", "format.date");
		}
		if(DateUtil.fromString(this.startDate, this.i18n("format.date")).after(DateUtil.fromString(this.endDate, this.i18n("format.date")))) {
			this.sendKeyError("validator.greater", "endDate", "startDate");
		}
		if(Checker.isNull(this.shiftId)) {
			this.sendKeyError("validator.required", "shift");
		}
		
		Date startDateObject = DateUtil.fromString(this.startDate, this.i18n("format.date"));
		Date endDateObject = DateUtil.fromString(this.endDate, this.i18n("format.date"));
		
		Date fromDate = DateUtil.fromString(this.fromDate, this.i18n("format.date"));
		Date toDate = DateUtil.fromString(this.toDate, this.i18n("format.date"));
		
		Collection<Offering> offerings = OfferingService.INSTANCE.find(this.name, null, this.levelId, null, this.shiftId, startDateObject, endDateObject, fromDate, toDate, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
		if(Checker.isNull(this.id)) {
			if(!offerings.isEmpty()) {
				this.sendKeyError("validator.exist", "offering");
			}
		}
		else {
			if(!offerings.isEmpty() && !offerings.contains(offering)) {
				this.sendKeyError("validator.exist", "offering");
			}
		}
		if(!Checker.isEmpty(this.midtermWrittenDate)) {
			if(!Checker.isDate(this.midtermWrittenDate, this.i18n("format.date"))) {
				this.sendKeyError("validator.mask", "midtermWrittenDate", "format.date");
			}
		}
		if(!Checker.isEmpty(this.finalSpokenDate)) {
			if(!Checker.isDate(this.finalSpokenDate, this.i18n("format.date"))) {
				this.sendKeyError("validator.mask", "finalSpokenDate", "format.date");
			}
		}
		if(!Checker.isEmpty(this.finalWrittenDate)) {
			if(!Checker.isDate(this.finalWrittenDate, this.i18n("format.date"))) {
				this.sendKeyError("validator.mask", "finalWrittenDate", "format.date");
			}
		}
		if(!Checker.isEmpty(this.cambridgeSpokenDate)) {
			if(!Checker.isDate(this.cambridgeSpokenDate, this.i18n("format.date"))) {
				this.sendKeyError("validator.mask", "cambridgeSpokenDate", "format.date");
			}
		}
		if(!Checker.isEmpty(this.cambridgeWrittenDate)) {
			if(!Checker.isDate(this.cambridgeWrittenDate, this.i18n("format.date"))) {
				this.sendKeyError("validator.mask", "cambridgeWrittenDate", "format.date");
			}
		}
		if(!Checker.isEmpty(this.issueDate)) {
			if(!Checker.isDate(this.issueDate, this.i18n("format.date"))) {
				this.sendKeyError("validator.mask", "certificateIssueDate", "format.date");
			}
		}
		return this.toSuccess();
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public Long getShiftId() {
		return shiftId;
	}

	public void setShiftId(Long shiftId) {
		this.shiftId = shiftId;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getMidtermWrittenDate() {
		return midtermWrittenDate;
	}

	public void setMidtermWrittenDate(String midtermWrittenDate) {
		this.midtermWrittenDate = midtermWrittenDate;
	}

	public String getFinalSpokenDate() {
		return finalSpokenDate;
	}

	public void setFinalSpokenDate(String finalSpokenDate) {
		this.finalSpokenDate = finalSpokenDate;
	}

	public String getFinalWrittenDate() {
		return finalWrittenDate;
	}

	public void setFinalWrittenDate(String finalWrittenDate) {
		this.finalWrittenDate = finalWrittenDate;
	}

	public String getIssueDate() {
		return issueDate;
	}

	public void setIssueDate(String issueDate) {
		this.issueDate = issueDate;
	}

	public Long getLevelId() {
		return levelId;
	}

	public void setLevelId(Long levelId) {
		this.levelId = levelId;
	}

	public String getFromDate() {
		return fromDate;
	}

	public void setFromDate(String fromDate) {
		this.fromDate = fromDate;
	}

	public String getToDate() {
		return toDate;
	}

	public void setToDate(String toDate) {
		this.toDate = toDate;
	}

	public String getCambridgeSpokenDate() {
		return cambridgeSpokenDate;
	}

	public void setCambridgeSpokenDate(String cambridgeSpokenDate) {
		this.cambridgeSpokenDate = cambridgeSpokenDate;
	}

	public String getCambridgeWrittenDate() {
		return cambridgeWrittenDate;
	}

	public void setCambridgeWrittenDate(String cambridgeWrittenDate) {
		this.cambridgeWrittenDate = cambridgeWrittenDate;
	}
}
